/**
 *====================================================
 * 文件名称: TransportNoticeServiceImpl.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2015年10月21日	chenxy(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 */
package com.yuexunit.finance.project.service;

import java.util.Date;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.core.BusinessException;
import com.yuexunit.finance.common.util.BeanUtils;
import com.yuexunit.finance.common.util.RandomUtil;
import com.yuexunit.finance.project.SettingConstants;
import com.yuexunit.finance.project.entity.FinanceBulkstockEntity;
import com.yuexunit.finance.project.entity.TransportNoticeEntity;
import com.yuexunit.finance.project.entity.lexicon.NoticeStateEnum;
import com.yuexunit.finance.project.entity.lexicon.ProductTypeEnum;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.util.DateUtil;

/**
 * @ClassName: TransportNoticeServiceImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2015年10月21日 19:32:48
 */
@Implements(TransportNoticeService.class)
public class TransportNoticeServiceImpl implements TransportNoticeService {

	@Override
	public Long createTransportNotice(TransportNoticeEntity entity) {
		entity.setNoticeState(NoticeStateEnum.UNCONFIRMED);
		entity.setTransportNoticeNumber(SettingConstants.PRE_NUMBER_NOTICE_TRANSPORT + RandomUtil.generateTimeNumber());
		entity.setEnabled(Boolean.TRUE);
		entity.setCreateDateTime(DateUtil.now());
		entity.setUpdateDateTime(DateUtil.now());
		PersistUtil.insert(entity);
		return entity.getTransportNoticeId();
	}

	@Override
	public void updateTransportNotice(TransportNoticeEntity entity) {
		TransportNoticeEntity persist = inquireTransportNoticeDetail(entity.getTransportNoticeId());
		BeanUtils.copyProperties(entity, persist);
		persist.setUpdateDateTime(DateUtil.now());
		PersistUtil.update(persist);
	}

	@Override
	public TransportNoticeEntity inquireTransportNoticeDetail(Long transportNoticeId) {
		TransportNoticeEntity entity = PersistUtil.get(TransportNoticeEntity.class, transportNoticeId);
		if (entity == null) {
			throw new BusinessException("未找到相应的运输通知");
		}
		return entity;
	}
	@Override
	public PageList<TransportNoticeEntity> inquireTransportNoticePageLists(List<Long> financeIds, Long financeId, String financeNumber, Long logisticsCompanyId, Date beginDate, Date endDate, Integer noticeState, String bulkstockName, PageCriteria page) {
		List<Long> financeIds2 = null;
		if (bulkstockName !=null && !"".equals(bulkstockName)) {// 若有搜索货物名称
			DetachedCriteria criteria = DetachedCriteria.forClass(FinanceBulkstockEntity.class);
			if (bulkstockName !=null && !"".equals(bulkstockName)) {
				criteria.add(Restrictions.like("bulkstockName", "%" + bulkstockName + "%"));
			}
			criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("financeId")));
			financeIds2 = PersistUtil.get(criteria);
			if (financeIds2.isEmpty()) {
				return new PageList<>(page);
			}
		}
	
		DetachedCriteria criteria = DetachedCriteria.forClass(TransportNoticeEntity.class);
		if (financeId != null) {
			criteria.add(Restrictions.eq("financeId", financeId));
		}
		if (logisticsCompanyId != null) {
			criteria.add(Restrictions.eq("logisticsCompanyId", logisticsCompanyId));
		}
		if (StringUtils.isNotBlank(financeNumber)) {
			criteria.add(Restrictions.like("financeNumber", "%" + financeNumber + "%"));
		}
		if (noticeState != null) {
			criteria.add(Restrictions.eq("noticeState", NoticeStateEnum.valueOf(noticeState)));
		}
		if (financeIds == null || financeIds.size() == 0) {
			return new PageList<TransportNoticeEntity>(page);
		}
		criteria.add(Restrictions.in("financeId", financeIds));
		
		if(financeIds2 !=null && financeIds2.size()>0 ){
			criteria.add(Restrictions.in("financeId", financeIds2));
		}
		if (beginDate != null) {
			criteria.add(Restrictions.ge("createDateTime", beginDate));
		}
		if (endDate != null) {
			criteria.add(Restrictions.le("createDateTime", endDate));
		}
		criteria.addOrder(Order.desc("createDateTime"));
		return PersistUtil.get(criteria, page);
	}
	
	@Override
	public PageList<TransportNoticeEntity> inquireTransportNoticePageList(Long financeId, String financeNumber, Long logisticsCompanyId, Date beginDate, Date endDate, Integer noticeState, Integer productType, PageCriteria page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(TransportNoticeEntity.class);
		if (financeId != null) {
			criteria.add(Restrictions.eq("financeId", financeId));
		}
		if (logisticsCompanyId != null) {
			criteria.add(Restrictions.eq("logisticsCompanyId", logisticsCompanyId));
		}
		if (StringUtils.isNotBlank(financeNumber)) {
			criteria.add(Restrictions.like("financeNumber", "%" + financeNumber + "%"));
		}
		if (noticeState != null) {
			criteria.add(Restrictions.eq("noticeState", NoticeStateEnum.valueOf(noticeState)));
		}
		if (productType != null && productType !=0) {
			criteria.add(Restrictions.eq("productType", ProductTypeEnum.valueOf(productType)));
		}
		if (beginDate != null) {
			criteria.add(Restrictions.ge("createDateTime", beginDate));
		}
		if (endDate != null) {
			criteria.add(Restrictions.le("createDateTime", endDate));
		}
		criteria.addOrder(Order.desc("createDateTime"));
		return PersistUtil.get(criteria, page);
	}

	@Override
	public List<TransportNoticeEntity> inquireTransportNoticeInProcess(Long logisticsCompanyId, Set<NoticeStateEnum> noticeStates) {
		DetachedCriteria criteria = DetachedCriteria.forClass(TransportNoticeEntity.class);
		if (logisticsCompanyId != null) {
			criteria.add(Restrictions.eq("logisticsCompanyId", logisticsCompanyId));
		}
		if (noticeStates.size() > 0) {
			criteria.add(Restrictions.in("noticeState", noticeStates));
		}
		return PersistUtil.get(criteria);
	}
}
